CIS 554 Prolog Quiz--Answer Key

Name ______________________________________

Remember that capitalization is important in Prolog. If you use incorrect capitalization, or write so that I cannot tell whether a word is capitalized, it's wrong.

Write Prolog predicates to express each of the following:
(Remember that a predicate consists of one or more clauses.)

  1. (1 point) Barack Obama is president of the USA.

    president(barak_obama).
    or
    president(barak_obama, usa).
    or similar.

  2. (1 points) Jack and Jill are the parents of Bill.

    This is really two facts, and should be represented as such.
    parent(jack, bill).
    parent(jill, bill).



  3. (1 point) Early to bed and early to rise, makes a man healthy, wealthy, and wise.

    This is really three rules, and should be represented as such.
    healthy(X) :- early_to_bed(X), early_to_rise ().
    wealthy(X) :- early_to_bed(X), early_to_rise ().
    wise(X) :- early_to_bed(X), early_to_rise ().
    Testing for man(X) is optional.

  4. (1 point) Use this predicate:  is_divisible_by(X, Y) :- X is Y * (X / Y).
    to write a predicate to test whether a year is a leap year. The rule is: A year is a leap year if (1) it is divisible by 4, and (2) is it not divisible by 100, unless (3) it is also divisible by 400. Thus, 2000 was a leap year, but 1900 was not.

    Many ways to write this. Here's one:
    leap_year(Year) :- divisible_by(Year, 4), \+divisible_by(100).

    leap_year(Year) :- divisible_by(Year, 400).


  5. (2 points) In Prolog, mod is an infix binary operator. Here are some other ways you might write is_divisible_by. For each, mark if it is correct or incorrect.

    1. is_divisible_by(X, Y) :- X mod Y = 0. incorrect

    2. is_divisible_by(X, Y) :- 0 = X mod Y. incorrect

    3. is_divisible_by(X, Y) :- X mod Y is 0.incorrect

    4. is_divisible_by(X, Y) :- 0 is X mod Y. correct

Unification and Logic

  1. (2 points) For each of the following, tell what value each variable receives as the result of the unification. Write "fail" if the unification does not succeed.

    1. [ace, deuce, trey] = [H | T]     H = ace, T = [deuce, trey]

    2. [Apple, pear] = [banana | Grape]    Apple = banana, Grape = [pear]


  2. (1 point) Resolve:
         ¬on_budget(X) ∨ ¬on_time(X) ∨ good_product(X).
         ¬good_product(Y)

    ¬on_budget(X) ∨ ¬on_time(X)


  3. (1 point) Rewrite the following Prolog clause as a logic clause:
         loves(chuck, X) :- female(X), rich(X).

    ¬female(X) ∨ ¬rich(X) ∨ loves(chuck, X)